//
//  TaskViewController.m
//  firstpoint
//
//  Created by sdajdhsakjhdsajk王王泽中 on 2017/6/28.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "TaskViewController.h"
#import "AllTaskViewController.h"
#import "LiShiTaskViewController.h"
#import "CanYuTaskViewController.h"
#import "YBPopupMenu.h"
#import "LeeMyShouCangRenWuViewController.h"
#import "LeeBaoChouZuiGaoRenWuViewController.h"//报酬最高
#import "LeeGaoDeViewController.h"//附近任务
#import "LeeNewTaskViewController.h"//新手任务
#import "LeeDailyTasksViewController.h"//日常
#import "XiTongRenWuViewController.h"
#define TITLES @[@"系统任务", @"报酬最高", @"新手任务",@"日常任务"]
#define ICONS  @[@"motify",@"delete",@"saoyisao",@"pay"]

@interface TaskViewController ()<UIScrollViewDelegate,YBPopupMenuDelegate>
{
    
    NSInteger   _currentIndex;
    NSDictionary*dataDic;
    NSMutableArray*nuMarr;
    NSArray*arrrrrrr;
}
@property (nonatomic, strong) YBPopupMenu *popupMenu;

@property (nonatomic,strong)UIButton *lastBtn;
@property(nonatomic,strong) UIScrollView *scrollView;//

@end

@implementation TaskViewController


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
      self.automaticallyAdjustsScrollViewInsets=NO;
    ;
  self.title=@"任务";
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNavBtn];
    [self initControl];
    [self addfristController];
    [self adddView];

    // Do any additional setup after loading the view.
}
//导航按钮
-(void)setNavBtn
{
    UIButton*rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,20,20)];
    [rightButton setImage:[UIImage imageNamed:@"rwgd"]forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightButtonClick:)forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    rightButton.imageView.contentMode = UIViewContentModeScaleAspectFit;

    self.navigationItem.rightBarButtonItem= rightItem;
    
    UIButton*leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0,0,25,25)];
    [leftBtn setImage:[UIImage imageNamed:@"rw01"]forState:UIControlStateNormal];
        [leftBtn addTarget:self action:@selector(searchprogram)forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*leftItme = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    leftBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;

    self.navigationItem.leftBarButtonItem= leftItme;

}
-(void)rightButtonClick:(UIButton*)btn
{
    [YBPopupMenu showRelyOnView:btn titles:TITLES icons:ICONS menuWidth:120 delegate:self];
    //推荐用这种写法
    
//    [YBPopupMenu showAtPoint:CGPointMake(btn.frame.origin.x, btn.frame.origin.y+40)  titles:TITLES icons:nil menuWidth:110 otherSettings:^(YBPopupMenu *popupMenu) {
//        popupMenu.dismissOnSelected = NO;
//        popupMenu.isShowShadow = YES;
//        popupMenu.delegate = self;
//        popupMenu.offset = 10;
//        popupMenu.type = YBPopupMenuTypeDark;
//        popupMenu.rectCorner = UIRectCornerBottomLeft | UIRectCornerBottomRight;
//    }];
//

}
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu
{
    NSLog(@"点击了 %@ 选项",TITLES[index]);
    if (index==0) {
        //系统任务
        XiTongRenWuViewController*vc=[XiTongRenWuViewController new];
        vc.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index==1) {
        //报酬最高
        LeeBaoChouZuiGaoRenWuViewController *vc = [LeeBaoChouZuiGaoRenWuViewController new];
        vc.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index==2) {
        //新手任务
        LeeNewTaskViewController *vc = [LeeNewTaskViewController new];
        vc.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index==3) {
        //日常
        LeeDailyTasksViewController*vc=[LeeDailyTasksViewController new];
        vc.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)adddView{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, width_screen, 40)];
    view.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:view];
    NSArray*arr=@[@"全部任务",@"参与任务",@"历史任务"];
    int aa = (width_screen-120)/4;
    for (int i=0; i<3; i++) {
        
        UILabel *tishiLable = [[UILabel alloc]initWithFrame:CGRectMake(aa+40+(aa+40)*i, 12, 30, 15)];
        tishiLable.backgroundColor = LRRGBColor(22, 141, 210);
        tishiLable.textColor = [UIColor whiteColor];
        tishiLable.layer.masksToBounds = YES;
        tishiLable.layer.cornerRadius = 7.0;
//        tishiLable.text = nuMarr[i];
        tishiLable.textAlignment = NSTextAlignmentCenter;
        tishiLable.font = [UIFont systemFontOfSize:11];
        // [view addSubview:tishiLable];
        
        
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(width_screen/3*i, 0,width_screen/3, 40);
        [button setTitle:arr[i] forState:UIControlStateNormal];
                [button setBackgroundColor:[UIColor whiteColor]];
        button.tag = 10+i;
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:LRRGBColor(68, 68, 68) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClicke:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
    }
    _lastBtn = [self.view viewWithTag:10];
    [_lastBtn setTitleColor:LRRGBColor(22, 141, 210) forState:UIControlStateNormal];
    UIView * _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 38,width_screen/3 ,2)];
    _lineView.tag = 1000;
    _lineView.backgroundColor = LRRGBColor(22, 141, 210);
    [view addSubview:_lineView];
}
-(void)buttonClicke:(UIButton *)button{
    
    [_lastBtn setTitleColor:LRRGBColor(68, 68, 68) forState:UIControlStateNormal];
    [button setTitleColor:LRRGBColor(22, 141, 210) forState:UIControlStateNormal];
    
    _lastBtn = button;
    
    [self yidongView];
    
    long a = button.tag-10;
    [self itemDidSelectedWithIndex:a withCurrentIndex:a];
    
    
}
-(void)searchprogram{
    //地图
    LeeGaoDeViewController *vc = [LeeGaoDeViewController new];
    vc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark--滚动视图
-(void)addBigScrollView{
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.frame = CGRectMake(0, 104, width_screen, height_screen-64-40-49);
    _scrollView.backgroundColor=LRRGBColor(235, 240, 243);
    _scrollView.contentSize=CGSizeMake(3 * width_screen, 0);
    _scrollView.delegate = self;
    _scrollView.showsVerticalScrollIndicator = FALSE;//隐藏横向滚动条
    _scrollView.showsHorizontalScrollIndicator = FALSE;//隐藏竖向滚动条
    _scrollView.alwaysBounceVertical = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO; //设置是否显示弹性效果
    _scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;//滑动
    [self.view addSubview:_scrollView];
    
}
#pragma mark Controller的创建
-(void)initControl{
    NSMutableArray *viewArray = [NSMutableArray array];
    AllTaskViewController *vc1= [AllTaskViewController new];
    CanYuTaskViewController *vc2 = [CanYuTaskViewController new];
    LiShiTaskViewController *vc3 = [LiShiTaskViewController new];
    
    [viewArray addObject:vc1];
    [viewArray addObject:vc2];
    [viewArray addObject:vc3];
    _subViewControllers = [NSArray array];
    _subViewControllers = viewArray;
}
//加载第一个
-(void)addfristController{
    [self addBigScrollView];
    
    UIViewController *viewController = (UIViewController *)_subViewControllers[0];
    viewController.view.frame = CGRectMake(0 , 10, width_screen , height_screen-104-49);
    [_scrollView addSubview:viewController.view];
    [self addChildViewController:viewController];
    
}
#pragma 滚动视图协议方法实现


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    _currentIndex = scrollView.contentOffset.x / width_screen;
    /** 当scrollview滚动的时候加载当前视图 */
    UIViewController *viewController = (UIViewController *)_subViewControllers[_currentIndex];
    viewController.view.frame = CGRectMake(_currentIndex * width_screen, 10, width_screen, _scrollView.frame.size.height);
    [_scrollView addSubview:viewController.view];
    [self addChildViewController:viewController];
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
{
    NSLog(@"scrollViewDidEndDecelerating");
    _currentIndex = scrollView.contentOffset.x / width_screen;
    UIButton *button = [self.view viewWithTag:_currentIndex+10];
    if (_lastBtn==button) {
        return;
    }else{
        [button setTitleColor:LRRGBColor(22, 141, 210) forState:UIControlStateNormal];
        [_lastBtn setTitleColor:LRRGBColor(68, 68, 68) forState:UIControlStateNormal];
        _lastBtn = button;
        [self yidongView];
    }
    
}
- (void)itemDidSelectedWithIndex:(NSInteger)index withCurrentIndex:(NSInteger)currentIndex
{
    if (currentIndex-index>=2 || currentIndex-index<=-2) {
        [_scrollView setContentOffset:CGPointMake(index * width_screen, 0) animated:NO];
    }else{
        [_scrollView setContentOffset:CGPointMake(index * width_screen, 0) animated:YES];
    }
}

-(void)yidongView
{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    UIView *_lineView = [self.view viewWithTag:1000];
    
    _lineView.frame = CGRectMake(_lastBtn.frame.origin.x, 38,width_screen/3 ,2);
    
    [UIView commitAnimations];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
